package com.leapp.sk.ui.activity.leave

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.view.View
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.example.mvp.view.TimePicker.OnDateSetListener
import com.example.mvp.view.TimePicker.TimePickerDialog
import com.example.util.LKTimeUtil
import com.example.util.addYear
import com.leapp.sk.R
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.viewdelegate.leave.LeaveEditDelegate

/**
 * 请假编辑界面
 * @author  chaohaipeng  on 2018/8/4.
 */
class LeaveEditActivity : BaseActivityMVP<LeaveEditDelegate>(), OnDateSetListener {


    /**
     * 时间选择器
     */
    private var mPickerDialog: TimePickerDialog? = null

    /**
     * 判断是开始时间还是结束时间
     */
    private var isStartTime=false
    /**
     * 开始时间
     */
    private var startTime:Long=0
    /**
     * 结束时间
     */
    private var endTime:Long=0
    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            viewDelegate.initLeaveDialog()
        }, R.id.rl_ale_type)
        viewDelegate.setOnClickListener(View.OnClickListener {
            isStartTime=true
            mPickerDialog?.show(fragmentManager, "info")
        }, R.id.rl_ale_start_time)
        viewDelegate.setOnClickListener(View.OnClickListener {
            if (TextUtils.isEmpty(viewDelegate.getStartDate())){
                LKToastUtil.showToastShort(resources.getString(R.string.string_leave_chose_start_time))
                return@OnClickListener
            }
            isStartTime=false
            mPickerDialog?.show(fragmentManager, "info")
        }, R.id.rl_ale_end_time)

        viewDelegate.setOnClickListener(View.OnClickListener {
          if (viewDelegate.contentCheck()){
              LKToastUtil.showToastShort("提交成功")
          }


        }, R.id.tv_ale_submit)
    }

    override fun initView(p0: Bundle?) {
        initSeleteDate()
        viewDelegate.setTitle(resources.getString(R.string.string_application_join_leave))
    }

    override fun initDate() {
    }

    override fun getDelegateClass(): Class<LeaveEditDelegate> {
        return LeaveEditDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }




    /**
     * 初始化时间选择器
     */
    private fun initSeleteDate() {
        var tenYears = addYear(-5)
        var endYears = addYear(5)
        mPickerDialog = TimePickerDialog.Builder()
                .setCallBack(this)
                .setCyclic(false)
                .setMinMillseconds(tenYears)
                .setThemeColor(ContextCompat.getColor(this, R.color.color_E0E0E0))
                .setTitleStringId(resources.getString(R.string.string_time_chose_title))
                .setWheelItemTextNormalColor(ContextCompat.getColor(this, R.color.color_A3A5A8))
                .setWheelItemTextSelectorColor(ContextCompat.getColor(this, R.color.color_474747))
                .setWheelItemTextSize(15)
                .setMaxMillseconds(endYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .build()
    }

    override fun onDateSet(timePickerView: TimePickerDialog?, millseconds: Long) {
        val formatTime = LKTimeUtil.getInstance().formatTime("yyyy-MM-dd HH:mm", millseconds)
        if (isStartTime){
            viewDelegate.setStartDate(formatTime)
            startTime=millseconds
        }else{
            endTime=millseconds
            if (endTime<startTime){
                LKToastUtil.showToastShort(resources.getString(R.string.string_leave_time_limt))
                return
            }
            viewDelegate.setEndDate(formatTime)

        }


    }


}